package DAO;


import Entity.ICcard;
import Entity.vipTab;
import util.Result;
import util.RfidTool;


import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class vipTabDao {
    private static short tsector = 1;
    private static short tblk = 6;

    static Connection connect = null;//数据库连接对象
    static PreparedStatement cmmd = null;//数据库命令执行对象
    static ResultSet rs = null;//数据集
    static String sql = "";


    RfidTool instance = RfidTool.getInstance();
    String cardID = instance.showCardId();//获取id卡的号码


    public String msg() {
        String msg = instance.getdeviceInti();
        System.out.println(msg);
        return msg;
    }

    public Result insert(vipTab in) {
        Result result = new Result();
        sql = "insert into vipTab values(?,?,?,?,?,?)";
        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);

            cmmd.setString(1, in.getIcID());
            cmmd.setString(2, in.getName());//传入参数的个数
            cmmd.setString(3, in.getIdentity());
            cmmd.setString(4, in.getTel());
            cmmd.setString(5, in.getSex());
            cmmd.setString(6, in.getAddress());

            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Result update3(vipTab in) {
        Result result = new Result();
        sql = "update vipTab set Name = '" + in.getName() + "',Identity= '" + in.getIdentity() + "',Tel= '" + in.getTel() + "',Sex= '" + in.getSex() + "',Address= '" + in.getAddress() + "'  where IcID = '" + in.getIcID() + "'";
        System.out.println(sql);
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.execute();
            System.out.println(sql);
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;

    }

    public Result Delete(String out) {
        Result result = new Result();
        sql = "delete from vipTab where IcID='"+out+"'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }

    public Result Querry(String x) {

        vipTab vip;
        Result result = new Result();
        List<vipTab> list = new ArrayList<>();


        sql = "Select * from vipTab where IcID like '%" + x + "%' OR Tel like '%" + x + "%' order by IcID+''";

        try {

            System.out.println(sql + "111");
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            //cmmd.setString(1,x);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                vip = new vipTab();
                vip.setIcID(rs.getString(1));
                vip.setName(rs.getString(2));
                vip.setIdentity(rs.getString(3));
                vip.setTel(rs.getString(4));
                vip.setSex(rs.getString(5));
                vip.setAddress(rs.getString(6));
                list.add(vip);
            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        result.setMessage(list);
        return result;
    }

    //通过id来查找会员信息
    public vipTab get_vip_byID(String id){
        vipTab vip=new vipTab();
        sql = "Select * from vipTab where IcID = '" + id + "'";

        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                vip.setName(rs.getString(2));
                vip.setIdentity(rs.getString(3));
                vip.setTel(rs.getString(4));
                vip.setSex(rs.getString(5));
                vip.setAddress(rs.getString(6));
            }
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null,"未获取到信息");
        }
        return vip;
    }
}
